const app = getApp();

Component({
  options: {
    multipleSlots: true
  },

  properties: {
    currTab: {
      type: Number,
      value: 0
    },
    clubID: Number,
    clubInfos: { type: Object, value: { name: "--------", own: "-----" } },
    dynamics: Object,
    member: Array
  },

  data: {
    clubInfos: {
      name: "--------",
      own: "-----"
    }
  },

  lifetimes: {
    attached() {
      this.setData({
        clubInfos: {
          name: "--------",
          own: "-----"
        }
      });
    }
  },

  methods: {
    // 关注该社团
    followClub(e) {
      let that = this;
      let data = {
        uid: app.globalData.userID,
        oid: e.currentTarget.dataset.id
      };
      console.log(data);
      app.action
        .followClub(data)
        .then(res => {
          console.log(res);
          if (res.code === 1000) {
            that.setData({
              [`clubInfos.followed`]: 1
            });
            app.globalData.followNum = app.globalData.followNum + 1;
          }
        })
        .catch(res => {
          console.log(res);
          app.setToast({ title: "关注失败！" });
        });
    },

    // 取消关注该社团
    unfollowClub(e) {
      let that = this;
      app.setModal({
        content: "确定取消关注？",
        confirm() {
          let data = {
            uid: app.globalData.userID,
            oid: e.currentTarget.dataset.id
          };
          console.log(data);
          app.action
            .unfollowClub(data)
            .then(res => {
              console.log(res);
              if (res.code === 1000) {
                that.setData({
                  [`clubInfos.followed`]: 0
                });
                app.globalData.followNum = app.globalData.followNum - 1;
              }
            })
            .catch(res => {
              console.log(res);
              app.setToast({ title: "取消关注失败！" });
            });
        }
      });
    },

    swiperTab(e) {
      let that = this;
      that.setData({
        currTab: e.detail.current
      });
      if (e.detail.current === 1) {
        if (this.data.dynamics) {
          let data = { oid: that.data.clubID };
          app.action.getClubDynamics(data).then(res => {
            console.log(res);
          });
        }
      }
    },

    clickTab(e) {
      let that = this;
      if (this.data.currTab === parseInt(e.target.dataset.current)) {
        return;
      } else {
        that.setData({
          currTab: e.target.dataset.current
        });
      }

      if (e.detail.current === 1) {
        if (this.data.dynamics) {
          let data = { oid: that.data.clubID };
          app.action.getClubDynamics(data).then(res => {
            console.log(res);
          });
        }
      }
    }
  }
});
